﻿#region License
// Copyright (c) 2009 Shengmin Zhang
//
// Permission is hereby granted, free of charge, to any person
// obtaining a copy of this software and associated documentation
// files (the "Software"), to deal in the Software without
// restriction, including without limitation the rights to use,
// copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the
// Software is furnished to do so, subject to the following
// conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
// OTHER DEALINGS IN THE SOFTWARE.
#endregion

using System;
using System.Web;
using System.IO.Compression;
using System.IO;
using System.Web.Caching;

using Apprender.Utilities;
using Apprender.Web.Configuration;

namespace Apprender.Web.Modules
{
    public sealed class CompressionModule:IHttpModule
    {

        #region IHttpModule Members

        public void Dispose()
        {
            
        }

        public void Init(HttpApplication context)
        {
            context.PostReleaseRequestState += new EventHandler(context_PostReleaseRequestState);
        }

        void context_PostReleaseRequestState(object sender, EventArgs e)
        {
            var section = ApprenderWebSectionGroup.CompressionModule;
            if (section == null)
                return;

            var app = sender as HttpApplication;
            var request = app.Request;

            bool useGZip = UHttp.IsGZipSupported(request);

            if (useGZip)
            {
                var response = app.Response;
                var exts = section.Extensions;
                var ext = Path.GetExtension(request.FilePath);
                if (IsExtIncluded(exts,ext))
                {
                    response.Filter = new GZipStream(response.Filter,
                                          CompressionMode.Compress);
                    response.AppendHeader("Content-Encoding", "gzip");
                    
                    response.Cache.VaryByHeaders["Accept-Encoding"] = true;
                }
            }           
        }

        private bool IsExtIncluded(string exts, string ext)
        {            
            var extsArray = exts.Split(',');

            for (int i = 0; i < extsArray.Length; i++)
            {
                var extFromArray = extsArray[i].Trim();
                if (extFromArray == ext)
                    return true;
            }

            return false;
        }

        #endregion
    }
}
